package com.mars.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.ticket.common.constant.Constant;
import com.mars.ticket.exception.WorkException;
import com.mars.ticket.request.SysTicketRequest;
import com.mars.ticket.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mars.ticket.service.ITicketService;
import com.mars.ticket.mapper.TicketMapper;
import com.mars.ticket.entity.Ticket;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;


/**
 * Ticket 实现层
 *
 * @Author wq
 * @Date 2021-03-15
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements ITicketService {


    @Override
    public void add(SysTicketRequest request) {
        if (StringUtils.isEmpty(request.getStartingPoint())) {
            throw new WorkException("请输入出发地");
        }
        if (StringUtils.isEmpty(request.getEndingPoint())) {
            throw new WorkException("请输入目的地");
        }
        if (Objects.isNull(request.getStartingTime())) {
            throw new WorkException("请输入开始时间");
        }
        if (Objects.isNull(request.getTrainNumber())) {
            throw new WorkException("请输入车次号");
        }
        if (Objects.isNull(request.getTrainNumberType())) {
            throw new WorkException("请输入车次类型");
        }
        Ticket ticket = new Ticket();
        ticket.setCreateTime(new Date());
        ticket.setSeatType(request.getSeatType());
        ticket.setStatus(Constant.ONE);
        ticket.setPrice(request.getPrice());
        ticket.setTicketNumber(request.getTicketNumber());
        ticket.setStartingTime(request.getStartingTime());
        ticket.setStartingPoint(request.getStartingPoint());
        ticket.setEndingPoint(request.getEndingPoint());
        ticket.setTrainNumberType(request.getTrainNumberType());
        ticket.setPassStation(request.getPassStation());
        ticket.setTrainNumber(request.getTrainNumber());
        ticket.setUpdateTime(new Date());
        save(ticket);
    }

    @Override
    public void delete(Integer id) {
        Ticket ticket = baseMapper.selectById(id);
        if (Objects.isNull(ticket)) {
            throw new WorkException("数据不存在");
        }
        if (ticket.getStatus().equals(Constant.ONE)) {
            throw new WorkException("该票已在线不能删除哦");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void updateTicket(SysTicketRequest request) {
        Ticket ticket = new Ticket();
        BeanUtils.copyProperties(request, ticket);
        baseMapper.updateById(ticket);
    }

    @Override
    public PageInfo<Ticket> listTicket(SysTicketRequest request) {
        IPage<Ticket> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<Ticket> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(request.getSeatType()) && request.getSeatType() != 0) {
            wrapper.eq(Ticket::getSeatType, request.getSeatType());
        }
        if (Objects.nonNull(request.getTrainNumberType()) && request.getTrainNumberType() != 0) {
            wrapper.eq(Ticket::getTrainNumberType, request.getTrainNumberType());
        }

        if (StringUtils.isNotEmpty(request.getEndingPoint())) {
            wrapper.like(Ticket::getEndingPoint, request.getEndingPoint());
        }
        if (StringUtils.isNotEmpty(request.getStartingPoint())) {
            wrapper.like(Ticket::getStartingPoint, request.getStartingPoint());
        }
        if (StringUtils.isNotEmpty(request.getStartingPoint()) && StringUtils.isNotEmpty(request.getEndingPoint())) {
            wrapper.orderByAsc(Ticket::getPrice);
        }
        wrapper.orderByDesc(Ticket::getCreateTime);
        IPage<Ticket> ticketIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(ticketIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), ticketIPage.getRecords());
        }
        return null;
    }

    @Override
    public Ticket acquire(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void orderTicket(Integer userId, Integer id) {

    }
}